import * as YvanUI from 'yvan-ui-wotu'
import CustomData from "./custom.data";

const _ = YvanUI.lodash

interface Input {

}

interface Ref {
    customGrid: YvanUI.Grid,
    errorGrid: YvanUI.Grid,
}

interface Output {

}

export class SimpleImportView extends YvanUI.ImportViewBase<Input, Output, Ref> {

    static define(option: YvanUI.ImportViewOption<Input, Output>) {
        return {
            content: SimpleImportView,
            ...option
        }
    }

    errorData: {errorId: string, rowNumber: string, errormessage: string}[] = []

    reloadData(importResult: ImportResult){
        super.reloadData(importResult);
        this.refs.customGrid.refresh();
    }



    vjson = {
        title: '客户导入',
        view: 'widget',
        width: '80%',
        items: [
            {
                view: 'tabs',
                defaultTabIndex: 0,
                items: [
                    {
                        title: '客户信息',
                        content: {
                            view: 'grid',
                            ff: 150,
                            autoSizeColumns: false,
                            idField: "customerid",
                            ref: 'customGrid',
                            dataSource: {
                                func: () => this.importResult.allData
                            },
                            onDataSourceAfter: (sender: YvanUI.Grid, dataOrgin: any[], raw: any, api: any) => {
                                const offset = (sender.currentPage - 1) * sender.pageSize
                                const endOffset = (offset + sender.pageSize > dataOrgin.length) ? dataOrgin.length : offset + sender.pageSize
                                return {
                                    pagination: {
                                        current: sender.currentPage,
                                        total: dataOrgin.length,
                                        size: sender.pageSize
                                    },
                                    data: dataOrgin.slice(offset, endOffset)
                                }
                            },
                            columns: [
                                {field: "customerid", hidden: true},
                                {field: "customercode", title: "客户编号", width: 80},
                                {field: "customername", title: "客户名称", width: 120},
                                {field: "logogram", title: "助记码", width: 80},
                                {field: "industryclass", title: "客户类型", width: 100},
                                {field: "customertype", title: "客户类别", width: 100},
                                {field: "zip", title: "邮编", width: 80},
                                {field: "contact", title: "联系人", width: 80},
                                {field: "telephone", title: "电话", width: 80},
                                {field: "province", title: "省", width: 80},
                                {field: "city", title: "市", width: 80},
                                {field: "address", title: "地址", width: 100},
                                {field: "ownername", title: "所属货主", width: 100},
                                {field: "isdefaultowner", title: "是否默认货主", width: 120},
                                {field: "routeid", hidden: true},
                                {field: "routename", title: "线路名称", width: 120},
                                {field: "isenable", title: "是否启用", width: 100},
                                {field: "remark", title: "备注", width: 100},
                            ],
                            onRowDblClick: "onRowDblClick",
                            onKeyDown: "onKeyDown"
                        }
                    },
                    {
                        title: '导入异常数据',
                        content: {
                            view: 'grid',
                            ff: 150,
                            autoSizeColumns: false,
                            idField: "errorId",
                            ref: 'errorGrid',
                            dataSource: {
                                func: () => this.importResult.errorMsgData
                            },
                            onDataSourceAfter: (sender: YvanUI.Grid, dataOrgin: any[], raw: any, api: any) => {
                                const offset = (sender.currentPage - 1) * sender.pageSize
                                const endOffset = (offset + sender.pageSize > dataOrgin.length) ? dataOrgin.length : offset + sender.pageSize
                                return {
                                    pagination: {
                                        current: sender.currentPage,
                                        total: dataOrgin.length,
                                        size: sender.pageSize
                                    },
                                    data: dataOrgin.slice(offset, endOffset)
                                }
                            },
                            columns: [
                                {field: "errorId", hidden: true},
                                {field: "rowNumber", title: "数据行号", width: 80},
                                {field: "errormessage", title: "错误信息", width: 200},
                            ]
                        }
                    }
                ]
            },
        ]
    }

    onButtonConfirmClick() {
        const row = this.refs.thegrid.getFocusedRow()
        if (!row) {
            YvanUI.showWarning('请选中一行数据')
            return
        }

        return this.confirm(row as Output)
    }

    onRowDblClick(sender: YvanUI.Grid, data: any) {
        this.confirm(data)
    }

    onKeyDown(sender: YvanUI.Grid, param: any) {
        const KEY_ENTER = 13

        if (param.event.keyCode === KEY_ENTER) {
            param.event.stopPropagation()
            param.event.preventDefault()
            this.onButtonConfirmClick()
            return true
        }
    }
}